#define __PIC24F04KA200__
#include "p24F04KA200.h"
#include "../common/uart.h"
#include "adc.h"
#include "tc77.h"
#include "system.h"
#include "../common/util.h"
#include "parser.h"
/*
#include <stdio.h>
 */

#define _ISR __attribute__((interrupt))

#define N_VIRTUAL_TIMERS 3

void t4();
void t5();
void t6();

unsigned timers[] = {0, 0, 0};
unsigned timer_period[] = {1, 1, 1};
void (*timer_functions[])() = {t4, t5, t6};

char buf[10];

/*
void testBauds(unsigned start, unsigned end) {
    unsigned i, j;
    j = start;
    while (1) {
        unsigned err = 0;

        for (i = 0; i < 1000; i++) {
            unsigned char val = (unsigned char) read();
            if (val != 'U') err++;
        }

        writeStrUnsLn("# errors (in 1000): ", err);

        if (j > end) j = start;

        U1BRG = j;
        writeStrUnsLn("Baud rate is: ", j);
        j++;
    }
}
*/

int main() {
    unsigned i, j = 0;

    LED_TRIS = TX_TRIS = 0;
    RX_TRIS = 1;
    LED = 0;

    adc_init();
    /*
        uart_init(BAUD_RATE_600);
     */
    uart_init(BAUD_RATE_1200,
            UART_BRGH_ENABLE,
            UART_RXINV_ENABLE,
            UART_UTXINV_ENABLE);

/*
    testBauds(98, 107);
*/
    
    /*
        tc77_init();
     */

    /*
        IFS0bits.T1IF = 0;
        IEC0bits.T1IE = 1;

        PR1 = 0xFF;

        T1CONbits.TCS = 0;
        T1CONbits.TCKPS0 = 1;
        T1CONbits.TCKPS1 = 1;
        T1CONbits.TGATE = 0;
        T1CONbits.TSIDL = 0;
     */

    /*
        rf_on();
        writeStr("\r\nSensor ");
        writeStr(itoa(ID, buf));
        writeStr(" is alive\r\n");
        rf_off();
     */

    /*
        for (i = 0; i < 100; i++) {
            for (j = 0; j < 200; j++);
            writeStr("STARTED\r\n");
        }
     */

/*
        while (1)
        writeStr("ALIVE\r\n");
*/

        parser_start();

    /*
        while (1)
            for (i = 0; i < 255; i++) {
                writeStr(itoa(i, 10));
                writeStr("\r\n");
                for (j = 0; j < 10000; j++);
            }
     */

    /*
        while (1) {
            unsigned start = 100;
            for (i = 0; i < 9; i++) {
                U1BRG = start + i;
                writeStr("UUUUUUUUUUUUUUUUUUU\r\n");
                for (j = 0; j < 20; j++) {
                    writeStr(itoa(i, buf));
                    writeStr("\r\n");
                }
                for (j = 0; j < 20; j++) {
                    writeStr("#|0|2|0|3");
                    writeStr("\r\n");
                }
            }
        }
     */

    while (1) {
        /*
                unsigned last = 0;
         */
        /*
                        for (j = 0; j < 20000; j++);
         */
        /*
                        writeStr("ALIVE\r\n");
         */
/*
        for (i = 0; i < 255; i++)
            write(i);
*/
                write(read());
        /*
                while (1) {
                    j = (unsigned char) read();
                    if (j != last) {
                        writeStrUnsLn("N consecutive reads=", i);
                        i = 0;
                        last = 0;
                    } else {
                        i++;
                        last = j;
                    }
                }
         */
        /*
                writeStr(itoa((unsigned char) read(), 10));
         */

        /*
                write('0'+RX);
         */
    }
}

void _ISR _T1Interrupt(void) {
    unsigned i = 0;
    IFS0bits.T1IF = 0;

    for (i = 0; i < N_VIRTUAL_TIMERS; i++) {
        timers[i]++;
        if (timers[i] == timer_period[i]) {
            timer_functions[i]();
            timers[i] = 0;
        }
    }
}

void t4() {
}

void t5() {
}

void t6() {
}

void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void) {
    IFS0bits.U1RXIF = 0;
    char data;
    data = U1RXREG;
    write(data);
    writeStr("INT\r\n");
}
